package com.pjb.springbootjjwt;

import com.pjb.springbootjjwt.netty4.NettyClient;
import com.pjb.springbootjjwt.netty4.NettyServer;
import com.pjb.springbootjjwt.netty4.Response;
import com.pjb.springbootjjwt.store.Main;

import java.net.InetSocketAddress;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class StoryClient {

    public static String cmd(String param) throws Exception {
        return Main.cmd(param);
    }

    public static void main(String[] args) throws InterruptedException {
        FutureTask<String> f = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                NettyServer.getInstance().start(new com.pjb.springbootjjwt.netty4.Callable() {
                    @Override
                    public String call(String param) {
                        try {
                            return cmd(param);
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                            return "fail";
                        }
                    }
                }, new InetSocketAddress("127.0.0.1", 3066));
                return "success";
            }
        });
        new Thread(f).start();

        System.out.println("welcome! this is story.");
        while (true) {
            try {
                Scanner scanner = new Scanner(System.in);
                String cmd = scanner.nextLine();
                Response r = NettyClient.getInstance().start(cmd, new InetSocketAddress("127.0.0.1", 3066));
                System.out.println("r:" + r);
            } catch (Exception e) {
                System.out.println("error:" + e);
            }
        }
    }
}
